<?php
/**
 * Created by JetBrains PhpStorm.
 * User: evan
 * Date: 13-3-5
 * Time: 下午6:19
 * To change this template use File | Settings | File Templates.
 */
class Message extends MY_Agent_Controller
{
    function __construct()
    {
        parent::__construct();
    }

    public function index()
    {
        $limit = 20;
        $currentPage = (int)$this->input->get_post('per_page');
        $offset = ($currentPage < 1) ? 0 : ($currentPage - 1) * $limit;


        $uName  = $this->input->get_post('uname');
        $time   = $this->input->get_post('create_time');
        $type   = $this->input->get_post('type');
        $status = $this->input->get_post('status');


        $where = array();
        $uName && $where['be_uname'] = $uName;
        ($type || $type === '0')  && $where['type'] = $type;
        ($status || $status === '0') && $where['status'] = $status;

        if ($time)
        {
            $eTime = explode('-', $time);//echo $eTime[0].'<br>';p(date('Y-m-d H:i:s', strtotime($eTime[0])));
            $where['create_time >='] = date('Y-m-d H:i:s', strtotime($eTime[0]));
            $where['create_time <='] = date('Y-m-d ', strtotime($eTime[1])).'23:59:59';
        }


        $this->load->model('Model_message', 'message');

        $totalNum = $this->message->count($where);
        $messageInfo = $this->message->lists($offset, $limit, $where);


        $this->load->library('pagination');
        $config = $this->page_config('Message/index', $totalNum, $limit, http_build_query($where));
        $this->pagination->initialize($config);
        $pageHtml = $this->pagination->create_links();


        $this->load->model('model_user', 'user');
        $userData = $this->user->getUser(100000, 0, 'uname');
        $messageData = $this->message->lists(0, 100000);


        $data = array(
            'title'        => '消息列表',
            'message_info' => $messageInfo,
            'messageData'  => $messageData,
            'user_data' => $userData,
            'pageHtml'  => $pageHtml,
            'time'      => $time,
            'status'    => $status,
            'uname'     => $uName,
            'type'      => $type,
            'totalNum'  => $totalNum,
        );

        $this->load->view('agent/message/index', $data);
    }

    public function create()
    {
        $this->load->model('model_message', 'message');
        $categoryData = array();//$this->message->getMessageCategory(1000);


        $this->load->model('model_user', 'user');
        $userData = $this->user->getUser(100000, 0, 'uname');


        $data = array(
            'title' => '发送消息',
            'category_data' => $categoryData,
            'user_data' => $userData,
        );


        $this->load->view('agent/message/create', $data);
    }


    public function delete()
    {
        $response = array('code' => '0', 'msg' => '删除成功');

        do {
            $mid = (int)$this->input->get_post('mid');
            $status = (int)$this->input->get_post('status');

            if (!$mid)
            {
                $response = error(100000);
                break;
            }


            $this->load->model('model_message', 'message');
            $item = $this->message->getById($mid);

            if (empty ($item))
            {
                $response = error(800000);
                break;
            }


            $messageStatus = config_item('message_status');
            if (!array_key_exists($status, $messageStatus))
                $status = MESSAGE_STATUS_NORMAL;


            $res = $this->message->save(array('status' => $status), $mid);
            if (!$res)
            {
                $response = error(800001);
                break;
            }
        }while (false);


        $this->json_output($response);
    }

    public function send()
    {
        $send_type = (int)$this->input->get_post('send_type');
        $uname     = trim($this->input->get_post('uname'));
        $message   = trim($this->input->get_post('message'));


        if (empty ($message))
            show_error('消息内容为空!');


        $uid = 0;
        if (1 == $send_type)
        {
            $this->load->model('model_user', 'user');
            $uItem = $this->user->getOne(array('uname' => $uname));


            if (empty ($uItem))
                show_error('用户不存在');


            $uid = $uItem['uid'];
        }


        $data = array(
            'type' => MESSAGE_TYPE_SYSTEM,
            'be_uid' => 0,
            'be_uname' => '系统',
            'to_uid' => $uid,
            'to_uname' => $uname,
            'message' => $message,
            'status' => MESSAGE_STATUS_NORMAL,
        );

        $this->load->model('model_message', 'message');

        $rs = $this->message->save($data);
        if (!$rs)
            show_error('消息发送失败!');

        $this->load->helper('url');
        redirect(url('admin').'message/index');
    }
}
